package com.elinkway.infinitemovies.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.adapter.RankDetailGridAdapter;
import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.bean.RankList;
import com.elinkway.infinitemovies.bigdata.BigDataConstants;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.RankListParser;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.le123.ysdq.R;
import com.lvideo.http.bean.LVideoDataHull;
import com.umeng.analytics.MobclickAgent;

public class RankDetailActivity extends BaseLoadingActivity {
	public static final String TAG = "RankDetailActivity";
	private GridView mGridView;
	private Bundle bundle;
	private RankDetailGridAdapter mGridAdapter;
	private String vt;
	private String title;
	private RankList rankList;
	private RequestRankListTask requestTask;
	private RelativeLayout root;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setLoadingContentView(R.layout.activity_rank_detail);
		bundle = getIntent().getExtras();
		vt = bundle.getString("vt");
		setCurrentPageId(vt);
		title = MoviesConstant.getVtName(vt)+"热播榜";
		findView();
		setListener();
		requestTask=new RequestRankListTask(this);
		requestTask.start();
		setOnLoading();
		MoviesApplication.getInstance().setTopActivity(this);
	}

	private void findView() {
		mGridView = (GridView) findViewById(R.id.rank_detail_grid);
		mTitleTv.setText(title);
	}
	private void setListener(){
		mGridView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				
				String aid = rankList.getRankList().get(position).getAlbumid();
				String title = rankList.getRankList().get(position).getName();
				String vt  = rankList.getRankList().get(position).getVt();
				VideoDetailActivity.launch(RankDetailActivity.this, aid, vt, title,"", "", "", "0","","");
			}
		});
	}
	@Override
	protected void onLoadingRefresh() {
		// TODO Auto-generated method stub
		if(requestTask!=null){
			requestTask.cancel();
		}
		requestTask = new RequestRankListTask(this);
		requestTask.start();
	}

	public static void launch(Activity activity, String vt) {
		launch(activity, vt, false);
	}

	public static void launch(Activity activity, String vt, boolean isfrompush) {
		Context context = activity;
		if (context == null) {
			context = MoviesApplication.getInstance();
		}
		Intent intent = new Intent(context, RankDetailActivity.class);
		intent.putExtra("vt",vt);
		intent.putExtra(MoviesApplication.START_FROM_NOTIFICATION, isfrompush);
		if (!(context instanceof Activity)) {
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		}
		context.startActivity(intent);
	}
	
	private class RequestRankListTask extends MoviesHttpAsyncTask<RankList>{

		@Override
		public void netNull() {
			// TODO Auto-generated method stub
			setLoadingFailed();
			super.netNull();
		}

		@Override
		public void netErr(int updateId, String errMsg) {
			// TODO Auto-generated method stub
			setLoadingFailed();
			super.netErr(updateId, errMsg);
		}

		public RequestRankListTask(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		public LVideoDataHull<RankList> doInBackground() {
			// TODO Auto-generated method stub
			return MoviesHttpApi.requestRankListData(new RankListParser(), vt);
		}

		@Override
		public void onPostExecute(int updateId, RankList result) {
			// TODO Auto-generated method stub
			rankList = result;
			mGridAdapter = new RankDetailGridAdapter(RankDetailActivity.this, rankList);
			mGridView.setAdapter(mGridAdapter);
			setLoadingComplete();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MoviesApplication.getInstance().setLastPageClassName(TAG);
		MobclickAgent.onResume(this);
	}

	@Override
	public void dealWithUmeng() {

	}

	@Override
	public void dealWithVisitUmeng() {

	}

	@Override
	protected void onLoading() {
		// TODO Auto-generated method stub
		mGridView.setVisibility(View.GONE);
	}
	@Override
	protected void onLoadingFailed() {
		// TODO Auto-generated method stub
		mGridView.setVisibility(View.GONE);
	}
	@Override
	protected void onLoadingComplete() {
		// TODO Auto-generated method stub
		mGridView.setVisibility(View.VISIBLE);
	}

	@Override
	public void setCurrentPageId(String vt) {
		if("1".equals(vt)){
			currentPageId = "episodes" + BigDataConstants.PGV_CURRENT_ID_DETAIL_RANKING;
		}else if("2".equals(vt)){
			currentPageId = "movie" + BigDataConstants.PGV_CURRENT_ID_DETAIL_RANKING;
		}else if("3".equals(vt)){
			currentPageId = "cartoon" + BigDataConstants.PGV_CURRENT_ID_DETAIL_RANKING;
		}else if("4".equals(vt)){
			currentPageId = "variety" + BigDataConstants.PGV_CURRENT_ID_DETAIL_RANKING;
		}
	}

}
